package com.group2.view;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.JFormattedTextField;
import model.*;
import static model.netBase.IP;
//import com.group2.dao.LogOnDao;
import com.group2.util.StringUtil;



/*用户管理模块：
 * 注册界面*/

public class UserRegisterFrame extends javax.swing.JFrame {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel_SEU;
	private JLabel jLabel_main;
	private JButton jb_confirm;
	private JButton jb_reset;
	private JRadioButton jrb_admin;
	private JRadioButton jrb_student;
	private JPasswordField passwordTxt;
	private JFormattedTextField userNameTxt;
	private JPanel contentPane;

	public UserRegisterFrame() {
		Font font = new Font("Dialog", Font.PLAIN, 12);
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource) {
				UIManager.put(key, font);
			}
		}
		initComponents();
		int wW=this.getWidth();
		int wH=this.getHeight();
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		int sW=screenSize.width;
		int sH=screenSize.height;
		this.setLocation(sW/2-wW/2,sH/2-wH/2);
	}

	private void initComponents() {

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel_SEU = new JLabel();
		jrb_student = new JRadioButton();
		jrb_admin = new JRadioButton();
		jb_reset = new JButton();
		jb_confirm = new JButton();
		userNameTxt = new JFormattedTextField();
		passwordTxt = new JPasswordField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("虚拟校园系统客户端注册 VCampus Register");
		setResizable(false);
		setBounds(100, 100, 500, 390);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		jLabel1.setText("用户名");
		jLabel1.setFont(new Font("宋体", Font.PLAIN, 14));
		jLabel1.setBounds(110, 174, 48, 35);
		jLabel2.setText("密码");
		jLabel2.setFont(new Font("宋体", Font.PLAIN, 14));
		jLabel2.setBounds(110, 215, 48, 35);
		jLabel_SEU.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/SEU.png")));
		jLabel_SEU.setBounds(203,26,90,90);
		
		jLabel_main=new JLabel();
		jLabel_main.setFont(new Font("1", 1, 22));
		jLabel_main.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/sys.png")));
		jLabel_main.setText("Welcome to VCampus!");
		jLabel_main.setBounds(124,114,291,50);
		
		contentPane.add(jLabel2);
		contentPane.add(jLabel1);
		contentPane.add(jLabel_SEU);
		contentPane.add(jLabel_main);

		userNameTxt.setFont(new Font("宋体", Font.PLAIN, 14));
		userNameTxt.setColumns(10);
		userNameTxt.setBounds(180, 177, 192, 28);
		contentPane.add(userNameTxt);

		passwordTxt.setFont(new Font("宋体", Font.PLAIN, 14));
		passwordTxt.setEchoChar('*');
		passwordTxt.setBounds(180, 218, 192, 28);
		contentPane.add(passwordTxt);


		jb_confirm.setText("确认");
		jb_confirm.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/login.png")));
		jb_confirm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					jb_RegisterActionPerformed(evt);
				} catch (SocketException e) {
					throw new RuntimeException(e);
				}
			}
		});
		jb_confirm.setBounds(180, 270, 90, 32);
		contentPane.add(jb_confirm);

		jb_reset.setText("取消");
		jb_reset.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/reset.png")));
		jb_reset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_ResetActionPerformed(evt);
			}
		});
		jb_reset.setBounds(281, 270, 90, 32);
		contentPane.add(jb_reset);

	}


	private void jb_ResetActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
		new UserLogOnFrame().setVisible(true);//返回登录端						

	}


	private void jb_RegisterActionPerformed(java.awt.event.ActionEvent evt) throws SocketException {
		String userName = userNameTxt.getText();
		String password = new String(passwordTxt.getPassword());
		if (StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(this, "用户名不能为空!");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(this, "密码不能为空!");
			return;
		}
		try {
			Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
			//设置超时时间
			socket.setSoTimeout(3000);
			//构建IO
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			//向服务器传输用户名、密码
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write("12" + '\n' +"true" +'\n' + userName + '\n' + password + '\n');//待完善，身份暂时设置为true
			bw.flush();
			//接受注册信息
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			int flag = Integer.parseInt(br.readLine());
			System.out.println(flag);
			switch(flag){
				case 1:
					JOptionPane.showMessageDialog(this, "该用户已存在！");
					return;
				case 2:
					JOptionPane.showMessageDialog(this, "用户名（学号）不存在！");
					return;
				case 3:
					JOptionPane.showMessageDialog(this, "数据库异常！");
					return;
				case 0:
					JOptionPane.showMessageDialog(this, "注册成功！");
					this.dispose();
					new UserLogOnFrame().setVisible(true);
					break;
			}
			is.close();
			os.close();
			socket.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "连接异常!");
		}
	}
}
